/* ###################################### */
/* ###     Util Engine, by Kalith    ### */
/* ###################################### */
/*             Timers source              */
/*                                        */
/*                                        */

#include "Timers.h"
#include "TimeManager.h"

using namespace std;

namespace Util
{
    const Str PeriodicTimer::CLASS_NAME = "PeriodicTimer";
    const Str Timer::CLASS_NAME = "Timer";

    PeriodicTimer::PeriodicTimer( const Double& dDuration, StartType mType, const Bool& bTickFirst )
    {
        dDuration_ = dDuration;
        if (bTickFirst) dElapsed_ = dDuration;
        else dElapsed_ = 0.0;
        mType_ = mType;
        bPaused_ = true;
        bFirstTick_ = true;
        if (mType == START_NOW) Start();
    }

    Double PeriodicTimer::GetElapsed()
    {
        if (!bPaused_)
            return dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_;
        else
            return dElapsed_;
    }

    const Double& PeriodicTimer::GetPeriod() const
    {
        return dDuration_;
    }

    const Bool& PeriodicTimer::IsPaused() const
    {
        return bPaused_;
    }

    Bool PeriodicTimer::Ticks()
    {
        if ( (mType_ == START_FIRST_TICK) && bFirstTick_ )
        {
            Start();
            bFirstTick_ = false;
        }

        if (!bPaused_)
        {
            if ((dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_) >= dDuration_)
            {
                Zero();
                return true;
            }
            else
                return false;
        }
        else
        {
            if (dElapsed_ >= dDuration_)
                return true;
            else
                return false;
        }
    }

    void PeriodicTimer::Stop()
    {
        dElapsed_ = 0.0;
        bPaused_ = true;
    }

    void PeriodicTimer::Start()
    {
        if (bPaused_)
        {
            dStart_ = TimeManager::GetSingleton()->GetTime();
            bPaused_ = false;
        }
    }

    void PeriodicTimer::Pause()
    {
        if (!bPaused_)
        {
            dElapsed_ += TimeManager::GetSingleton()->GetTime() - dStart_;
            bPaused_ = true;
        }
    }

    void PeriodicTimer::Zero()
    {
        dElapsed_ = 0.0;
        dStart_ = TimeManager::GetSingleton()->GetTime();
    }

    Timer::Timer(const StartType& mType)
    {
        if (mType == START_PAUSED)
            bPaused_ = true;
        else
            dStart_ = TimeManager::GetSingleton()->GetTime();
    }

    void Timer::AddTime( const Double& dAdditionnalTime )
    {
        dStart_ -= dAdditionnalTime;
    }

    void Timer::SetTime( const Double& dTime )
    {
        dElapsed_ = dTime;
        dStart_ = TimeManager::GetSingleton()->GetTime();
    }

    Double Timer::GetElapsed()
    {
        if (!bPaused_)
            return dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_;
        else
            return dElapsed_;
    }

    const Bool& Timer::IsPaused() const
    {
        return bPaused_;
    }

    void Timer::Stop()
    {
        dElapsed_ = 0.0;
        bPaused_ = true;
    }

    void Timer::Start( const Bool& bReset )
    {
        dStart_ = TimeManager::GetSingleton()->GetTime();
        bPaused_ = false;
        if (bReset)
            dElapsed_ = 0.0;
    }

    void Timer::Pause()
    {
        if (!bPaused_)
        {
            dElapsed_ += TimeManager::GetSingleton()->GetTime() - dStart_;
            bPaused_ = true;
        }
    }

    void Timer::Zero()
    {
        dElapsed_ = 0.0;
        dStart_ = TimeManager::GetSingleton()->GetTime();
    }
}
